﻿//-----------------------------------------------------------------------
// <copyright file="ManipulatorHandle.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Base.Manipulator
{
    using TileTool.Common.Math;
    using TileTool.Input;
    using TileTool.Render;

    /// <summary>
    /// A manipulator handle is a piece of a manipulator that can be interacted with
    /// using the mouse. It has a rectangle which is the region in which it is
    /// expected to draw its visuals.
    /// </summary>
    public abstract class ManipulatorHandle
    {
        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public ManipulatorHandle()
        {
            this.Id = -1;
            this.Visible = true;
            this.Selected = false;
            this.TriggerManipulate = true;
            this.Pos = this.DragStartPos = Vector2D.Zero;
            this.SelectedColor = ColorRGBA.Black;
            this.IdleColor = ColorRGBA.Black;
        }

        #endregion

        #region Properties - Object creation time

        /// <summary>
        /// Gets or sets a user defined ID. The handle system will never access or
        /// change this ID. Its default value is -1.
        /// </summary>
        public int Id
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets if this handle triggers the object manipulation process.
        /// 
        /// If true, we trigger the manipulation process.
        /// If false, we don't trigger the process and are considered a utility handle.
        /// </summary>
        public bool TriggerManipulate
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the color to draw the handle with when it is selected.
        /// </summary>
        public ColorRGBA SelectedColor
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the color to draw the handle with when it is idle.
        /// </summary>
        public ColorRGBA IdleColor
        {
            get;
            set;
        }

        #endregion

        #region Properties - Runtime

        /// <summary>
        /// Gets or sets if the handle is visible.
        /// </summary>
        public bool Visible
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets if the handle is selected.
        /// </summary>
        public bool Selected
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the center position of the handle.
        /// </summary>
        public Vector2D Pos
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the position for the handle when a drag was started.
        /// 
        /// Will be set regardless of if this handle is being dragged.
        /// </summary>
        public Vector2D DragStartPos
        {
            get;
            set;
        }

        #endregion

        #region Properties - Protected

        /// <summary>
        /// Gets the color we should draw with based off of the Selected bool.
        /// </summary>
        protected ColorRGBA DrawColor
        {
            get
            {
                return this.Selected ? this.SelectedColor : this.IdleColor;
            }
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Test the passed point to see if it is inside of this handle.
        /// </summary>
        /// <param name="posLS">Mouse position in local space.</param>
        /// <returns>Returns true if the point is in the handle.</returns>
        public virtual bool TestPoint(Vector2D posLS)
        {
            return this.Visible;
        }

        /// <summary>
        /// Draw the manipulator handle. The manipulator's matrix will be pushed
        /// onto the matrix stack when draw is called.
        /// </summary>
        /// <param name="renderData">Draw data.</param>
        public abstract void Draw(IRenderData renderData);

        #endregion
    }
}
